package com.tbsoft.ehr.action.notify;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.model.NotifyFile;
import com.tbsoft.ehr.service.notify.NotifyService;
import com.tbsoft.ehr.util.ConfigProps;

/**
 * @desc 公告action
 * @author vastdream
 * 
 */
public class NotifyAction extends BaseAction {
	private Log log = LogFactory.getLog(NotifyAction.class);
	private NotifyService notifyService;

	private String fileId;
	private String fileName;
	private String scId;
	private String uploadFile;
	private String uploadFileFileName;

	public synchronized String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public synchronized void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public synchronized String getUploadFile() {
		return uploadFile;
	}

	public synchronized void setUploadFile(String uploadFile) {
		this.uploadFile = uploadFile;
	}

	public synchronized String getScId() {
		return scId;
	}

	public synchronized void setScId(String scId) {
		this.scId = scId;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public synchronized String getFileId() {
		return fileId;
	}

	public synchronized void setFileId(String fileId) {
		this.fileId = fileId;
	}

	private String notifyType;

	public String getNotifyType() {
		return notifyType;
	}

	public void setNotifyType(String notifyType) {
		this.notifyType = notifyType;
	}

	private List<NotifyFile> notifyList = new ArrayList<NotifyFile>();

	public List<NotifyFile> getNotifyList() {
		return notifyList;
	}

	public String getFile() {
		NotifyFile nf = notifyService.getNotifyFileById(fileId);
		try {
			fileName = new String(nf.getFileName().getBytes(),"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			return "error_file_name";
		}
		return "getFile";
	}

	public InputStream getFileInputStream() throws Exception {
		log.info("classpath:" + System.getProperty("java.class.path"));
		String path = (String) ConfigProps.getContextProperty("notify.file.path");
		String fileName = path + File.separator + fileId;
		FileInputStream fis = new FileInputStream(fileName);
		return fis;
	}

	public void setNotifyService(NotifyService notifyService) {
		this.notifyService = notifyService;
	}

	private static final long serialVersionUID = 1L;

	public String message() {
		notifyList.clear();
		notifyType = "M";
		loadFileList();
		return "message";
	}

	public String process() {
		notifyList.clear();
		notifyType = "P";
		loadFileList();
		return "message";
	}

	public String rules() {
		notifyList.clear();
		notifyType = "R";
		loadFileList();
		return "message";
	}
	
	public String selfdoc(){
		notifyList.clear();
		notifyType = "S";
		loadFileList();
		return "selfdoc";
	}

	private void loadFileList() {
		try {
			List<NotifyFile> tmpList = notifyService.getNotifyFileByType(notifyType);
			if (tmpList != null) {
				notifyList.addAll(tmpList);
			}
		} catch (Exception e) {
			log.error("Exception:", e);
		}
	}
	
	public String release(){
		return "browse";
	}
	public String list(){
		try {
			List<NotifyFile> tmpList = notifyService.getNotifyFileList(notifyType, null);
			if (tmpList != null) {
				notifyList.addAll(tmpList);
			}
		} catch (Exception e) {
			log.error("Exception:", e);
		}
		return "browse";
	}
	
	public String add(){
		return "add";
	}
	public String save(){
		log.info("save file");
		try{
			if(!notifyType.endsWith("S") && uploadFileFileName != null && !uploadFileFileName.toLowerCase().endsWith("pdf")){
				errorReturn("请上传pdf文件");
			}
			if(fileId == null || fileId.isEmpty()){
				notifyService.addNotifyFile(notifyType, fileName, scId, uploadFile);
			}else{
				notifyService.updateNotifyFile(fileId, notifyType, fileName, scId, uploadFile);
			}
		}catch(Exception e){
			log.error("Exception:", e);
			errorReturn("公告上传失败");
		}
		setRel("notify_release");
		return SUCCESS;
	}
	public String modify(){
		NotifyFile nf = notifyService.getNotifyFileById(fileId);
		this.fileName=nf.getFileName();
		this.notifyType=nf.getNotifyType();
		this.scId=nf.getScId();
		return "add";
	}
	public String delete(){
		log.info("delete file:"+this.fileId);
		try{
			notifyService.deleteNotifyFile(fileId);
		}catch(Exception e){
			log.error("delete exception:", e);
			errorReturn("公告删除失败");
		}
		setRel("notify_release");
		return SUCCESS;
	}

}
